package com.meiricai.app.utility;

import java.io.IOException;
import java.util.List;

import com.meiricai.app.R;
import com.meiricai.sdk.LotteryDrawClient;
import com.meiricai.sdk.models.LotteryDraw;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;

public class WorkerThreadRunnable implements Runnable {

	private ListView list;

	private Context context;

	private Handler mainThreadHandler = null;

	public WorkerThreadRunnable(Context context, Handler h, ListView list) {
		mainThreadHandler = h;
		this.context = context;
		this.list = list;
	}

	public void run() {
		Message msg = mainThreadHandler.obtainMessage();
		List<LotteryDraw> lotteryDraws;
		try {
			lotteryDraws = LotteryDrawClient.Instance.getLatestLotteryDraws(10,
					1);
			if (lotteryDraws != null && lotteryDraws.size() != 0) {
				final LotteryListAdapter adapter = new LotteryListAdapter(
						this.context, lotteryDraws);
				this.list.post(new Runnable() {
					public void run() {
						WorkerThreadRunnable.this.list.setAdapter(adapter);
					}
				});
			}
		} catch (IOException e) {
			mainThreadHandler.sendMessage(msg);
		} catch (Exception e) {
			mainThreadHandler.sendMessage(msg);
		}
	}
}
